import matplotlib.pyplot as plt
import numpy as np

t_min = -4
t_max = 4
n_min = -1000
n_max = 1000
t = np.arange(t_min, t_max+1, 0.01)
n = np.arange(n_min, n_max+1, 40)
cost = np.cos(2 * np.pi * t)
cosn = np.cos(n / 4)


# 自己定义函数y=x(t)
def x(t):
    y = [1 if (i >= 0) else 0 for i in t]

    return y


a = x(t)

plt.rcParams['font.sans-serif'] = ['SimHei']  # 加上这一句就能在图表中显示中文
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

plt.subplot(411)
plt.plot(t, a * cost)
plt.ylabel('cos(2πt)u(t)')

plt.subplot(413)
plt.stem(n, cosn)
plt.ylabel('cos(n / 4)')


plt.show()
